/**
 * @param {string} s1
 * @param {string} s2
 * @return {boolean}
 * 输入：s1 = "ab" s2 = "eidbaooo"
输出：true
解释：s2 包含 s1 的排列之一 ("ba").
 */
var checkInclusion = function (s1, s2) {
  const delChar = (char, index) => {
    const charArr = char.split('')
    charArr.splice(index, 1)
    return charArr.join('')
  }
  const len = s2.length
  const firstChar = s1.charAt(0)
  const index = s2.indexOf(firstChar)
  if (index === -1) return false
  let left = index === 0 ? -1 : index - 1
  let right = index === len ? -1 : index + 1
  let newChar = s1.slice(1)
  while (true) {
    let stateLeft = true
    let stateRight = true
    if (newChar.length === 0) {
      return true
    }
    if (left >= 0) {
      const leftI = newChar.indexOf(s2.charAt(left) || null)
      if (leftI === -1) {
        stateLeft = false
      } else {
        newChar = delChar(newChar, leftI)
      }
    } else {
      stateLeft = false
    }
    if (right < len) {
      const rightI = newChar.indexOf(s2.charAt(right) || null)
      if (rightI === -1) {
        stateRight = false
      } else {
        newChar = delChar(newChar, rightI)
      }
    } else {
      stateRight = false
    }
    left--
    right++
    if (!stateLeft && !stateRight) {
      return false
    }
  }
}

console.log(checkInclusion('ky', 'ainwkckifykxlribaypk'))
